package com.chzc.fm.duotin.ui.channel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.SpecificTopicAdapter;
import com.chzc.fm.duotin.bean.channel.Topic;
import com.chzc.fm.duotin.utils.DuotinTask;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.HttpUtil;
import com.chzc.fm.duotin.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Administrator on 2016/6/29.
 */
public class SpecificTopicActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvtitle;
    private ImageView ivback;
    private RecyclerView recyclerView;
    private SpecificTopicAdapter adapter;

    private String dataid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_specifictopic);

        initViews();
        initDatas();

    }

    public void initViews()
    {
        recyclerView = (RecyclerView) findViewById(R.id.specifictopic_content_rv);
        tvtitle = (TextView) findViewById(R.id.specifictopic_title_tv);
        ivback = (ImageView) findViewById(R.id.specifictopic_back_iv);

        LinearLayoutManager manager = new LinearLayoutManager(SpecificTopicActivity.this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        ivback.setOnClickListener(this);


    }

    public void initDatas()
    {

        Topic topicitem = (Topic) getIntent().getSerializableExtra("topicitem");
        dataid = topicitem.getId()+"";
        tvtitle.setText(topicitem.getTitle());

        getSpecialTopic(new DuotinTask.IRequestCallback() {
            @Override
            public void success(Object object) {

                try {

                    LogUtil.w("开始解析数据");

                    JSONObject root = new JSONObject(object.toString());
                    String error_msg = root.getString("error_msg");
                    if("success".equals(error_msg))
                    {
                        LogUtil.w("获取数据成功");

                        JSONObject data = root.getJSONObject("data");
                        JSONArray datalist = data.getJSONArray("data_list");

                        List<Topic> topicList = Topic.arrayTopicFromData(datalist.toString());

                        adapter = new SpecificTopicAdapter(SpecificTopicActivity.this,topicList);
                        recyclerView.setAdapter(adapter);


                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }


    /**
     * 获取具体的话题数据
     *
     * @param callback
     */
    public void getSpecialTopic(DuotinTask.IRequestCallback callback)
    {
        DuotinTask.IRequest request = new DuotinTask.IRequest() {
            @Override
            public Object doRequest() {
                LogUtil.w("GlobalParam.URL_SPECIFIC_HEAD + dataid + GlobalParam.URL_SPECIFIC_TAIL>>>" + GlobalParam.URL_SPECIFIC_HEAD + dataid + GlobalParam.URL_SPECIFIC_TAIL);
                return HttpUtil.doGet(GlobalParam.URL_SPECIFIC_HEAD + dataid + GlobalParam.URL_SPECIFIC_TAIL);
            }
        };

        new DuotinTask(request,callback).execute();
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.specifictopic_back_iv:
                finish();
                break;
        }
    }
}
